package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import model.BookmarkDAO;
import model.Model;
import model.UserDAO;

import org.mybeans.form.FormBeanFactory;

import dataBeans.Bookmark;
import formBean.GetListForm;

public class GetUserBookmarksAction extends Action {

	private FormBeanFactory<GetListForm> formBeanFactory = FormBeanFactory.getInstance(GetListForm.class);
	private UserDAO userDAO;
	private BookmarkDAO bookmarkDAO;
	
	public GetUserBookmarksAction(Model model){
		userDAO = model.getUserDAO();
		bookmarkDAO = model.getBookmarkDAO();
	}
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "getUserBookmarks.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		//System.out.println("I am in perform");
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        try {
        	GetListForm form = formBeanFactory.create(request);
        	request.setAttribute("form",form);
        	//System.out.println("--------------------------------I am in GetUserBookmarksAction");
        	if(form.getUsername()!= null){

        		System.out.println(form.getUsername());
        		Bookmark [] userBookmarks = bookmarkDAO.getUserBookmarks(userDAO.lookup2(form.getUsername()).getUserId());
        		if(userBookmarks.length < 1){
        			errors.add("No bookmarks for the user");
        		}
        		request.getSession().setAttribute("userBookmarksList", userBookmarks);
        		return "userBookmarkList.jsp";

        	}else{
        		errors.add("Null username");
        		return "error.jsp";
        	}
        }
		catch(Exception e){
			e.printStackTrace();
			return "error.jsp";
		}
		
	}

}
